﻿<%@ LANGUAGE="JavaScript" codepage=65001%><?xml version="1.0" encoding="UTF-8"?>
<!-- #include file="AAC.asp" -->
<!-- #include file="Config.asp" -->
<%
//==================================
//  Weather for google earth v1.0
//  更新时间: 2006-06-26
//  embont@gmail.com
//  www.dnxh.cn
//==================================
Response.ContentType = "text/xml";//"application/vnd.google-earth.kml+xml";
Response.CharSet = 65001 ;//[bboxWest],[bboxSouth],[bboxEast],[bboxNorth]
    
var alt  = Request.QueryString("CAMERA");
var BBOX = Request.QueryString("BBOX");
var _ary = String(BBOX).split(",");
var west    = _ary[0];
var south   = _ary[1];
var east    = _ary[2];
var north   = _ary[3];

var Vkey  = "7d248cae7bcf8618";//API关键字
var Vpar  = "1017780104";//用户ID
var fso = Server.CreateObject("Scripting.FileSystemObject");

var vSQL;
%>
<%
	if (alt>7000*1000){
	//取最高级别的点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<1";
	}else
	if (alt<7000*1000 && alt>4000*1000){
	//取最高级别的点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<2";
	}else 
	//取省级以上点
	if (alt<=4000*1000 && alt>1000*1000){
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<3";
	}else{
	//取全部点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+")";
	}
	var oAAC = new AAC(mdbfile,vSQL);
	oAAC.open(3,1);
	var oRS = oAAC.oRecordSet;
%><kml xmlns="http://earth.google.com/kml/2.2">
<Document>
	    <% 
	    //屏幕内未含有点，获取最近点
	    if (oRS.RecordCount == 0){
	    
		//清除前一个当前屏幕的记录集
	    oRS = "";
	    oAAC.close();
	    oAAC = "";
	    
		//新建一个记录集，用来筛选最近点。		
		var vAAC = new AAC(mdbfile,"gew");
		vAAC.open(3,1);
		var vRS = vAAC.oRecordSet;
		
        // 取得窗口中心点坐标位置
	    var Latitude  = (Number(south)+Number(north))/2;
	    var Longitude = (Number(west) +Number(east) )/2;
	    
		//最近点计算c*c=a*a+b*b
		var n = 0;
		var c = new Array();
				
		for(vRS.MoveLast();!vRS.BOF;vRS.MovePrevious()){
		n++;
		var vlon = vRS("lon");
		var vlat = vRS("lat");
		var ven = vRS("en");
		var vcn = vRS("cn");
		//计算最近距离
		var a   = Latitude - vlat;
		    a   = Math.abs(a)
		var b   = Longitude- vlon;
		    b   = Math.abs(b)		
		var c2  = a*a+b*b ;
		c.push(Math.sqrt(c2) + "|" + vRS("code")+ "|" + vcn + "|" + ven);
		}
		c.sort();
		var vcodes = c[0];
		var vcode  = vcodes.split("|");
		var code   = vcode[1];
		var cn = vcode[2];
		if(cn=="null"){
		cn=vcode[3];
		}
  //有缓存执行
  if(fso.FileExists(Server.MapPath("xml/"+code+".xml"))){
		%>
	<ScreenOverlay>
    <name>当前天气情况</name>
    <drawOrder>80</drawOrder>
    <Icon>
	<%
	//屏幕信息
        //=====XML==========================
        var xml = Server.CreateObject("Microsoft.XMLDOM");
        xml.async = false;
        xml.load(Server.MapPath("xml/"+code+".xml")); 
        //======Load XSL=========================
        var xsl = Server.CreateObject("Microsoft.FreeThreadedXMLDOM");
        xsl.async = false;
        xsl.load(Server.MapPath("style/centercity.xsl"));        
        //======Transform file====================
        //xml.transformNodeToObject( xsl , Response);
        var oXslt = Server.CreateObject("Msxml2.XSLTemplate");
            oXslt.stylesheet = xsl;
        var oXslP = oXslt.createProcessor();
            oXslP.addParameter("ServerURL",ServerURL);
            oXslP.input = xml;
            oXslP.output = Response;
			oXslP.transform();
		xsl = "";
        xml = "";
	//====
	%>
    </Icon>
    <overlayXY x="5" y="205" xunits="pixels" yunits="pixels"/>
    <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
    <size x="0" y="0" xunits="fraction" yunits="fraction"/>
    </ScreenOverlay>
	<NetworkLink>
	<open>1</open>
	<name>最近的一个城市观测点</name>
		<Link>
			<href><![CDATA[<%=ServerURL%>/marker.asp?code=<%=code%>&hl=cn&dnam=<%=encodeURI(cn)%>]]></href>
			<refreshMode>onInterval</refreshMode>
			<refreshInterval>3600</refreshInterval>
			<viewRefreshMode>onRequest</viewRefreshMode>
		</Link>
	</NetworkLink>
	<ScreenOverlay>
    <name>距观测点：<%=Math.floor(vcode[0]*100000)%>米</name>
    <drawOrder>81</drawOrder>
    <Icon>
      <href><![CDATA[<%=ServerURL%>/img/logo/cc.png]]></href>
    </Icon>
    <overlayXY x="0" y="190" xunits="pixels" yunits="pixels"/>
    <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
    <size x="0" y="0" xunits="fraction" yunits="fraction"/>
    </ScreenOverlay>
	    <%
	    }else{
	    //中心点未有缓存
	    }
	    vRS = "";
	    vAAC.close();
	    vAAC = "";
	    }else{
	    //遍历屏幕范围内的点
		for(oRS.MoveLast();!oRS.BOF;oRS.MovePrevious()){%>
	<%
     var code = oRS("code");
     var cn="";
         cn= oRS("cn")+"";
      if(cn=="undefined"||cn=="null"){cn=oRS("en")}
	//有缓存显示
    if(fso.FileExists(Server.MapPath("xml/"+code+".xml"))){
	%>
	<NetworkLink>
	<open>1</open>
	<name></name>
		<Link>
			<href><![CDATA[<%=ServerURL%>/marker.asp?code=<%=oRS("code")%>&hl=cn&dnam=<%=encodeURI(cn)%>]]></href>
			<refreshMode>onInterval</refreshMode>
			<refreshInterval>3600</refreshInterval>
			<viewRefreshMode>onRequest</viewRefreshMode>
		</Link>
	</NetworkLink>
	<% 
	}}
	oRS = "";
	oAAC.close();
	oAAC = "";
	} %>
	</Document>
</kml>